package com.example.perplonews.a_d_fragment.b_perplo.fragment

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.get
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.perplonews.R
import com.example.perplonews.a_b_utils.LjlUtils
import com.example.perplonews.a_d_fragment.b_perplo.Per_News_Activity
import com.example.perplonews.a_d_fragment.b_perplo.adapter.Perplo_Rec_Adapter
import com.example.perplonews.a_d_fragment.c_video.adapter.Small_Adapter
import com.example.perplonews.a_d_fragment.c_video.entity.Data
import com.example.perplonews.a_d_fragment.c_video.entity.SmallVideo
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class Recommond_Fragment : Fragment() {
    private val perploRec: RecyclerView by lazy { myview!!.findViewById<RecyclerView>(R.id.perplo_rec) }

    private var myview: View? = null
    private var adapter: Perplo_Rec_Adapter? = null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        myview = inflater.inflate(R.layout.fragment_recommond_, container, false)
        init()
        return myview
    }

    private fun init() {
        OkGo.get<String>("http://v.juhe.cn/toutiao/index?type=top&key=e3080ff4a57ed090c999dc0d9082dce4")
            .execute(object :
                StringCallback() {
                override fun onSuccess(response: Response<String>) {
                    var body = response.body()

                    var fromJson = Gson().fromJson(body, SmallVideo::class.java)

                    var data = fromJson.result?.data
                    if (data != null) {
                        var arrayList = data as ArrayList<Data>
                        adapter = Perplo_Rec_Adapter(arrayList)

                        perploRec.adapter = adapter
                        perploRec.layoutManager = LinearLayoutManager(context)
                        perploRec.addItemDecoration(
                            DividerItemDecoration(
                                context,
                                DividerItemDecoration.VERTICAL
                            )
                        )
                        adapter?.setOnItemClickListener { adapter, view, position ->
                            arrayList[position].isclick = true
                            adapter.notifyDataSetChanged()
                            var url = data[position].url
                            var intent = Intent(context, Per_News_Activity::class.java)
                            intent.putExtra("url", url)
                            startActivity(intent)


                        }
                    } else {
                        // TODO: 2021/6/30 数据请求失败的时候 加载 一个占位符       正常应该用  请求数据那边的方法
                        var inflate =
                            LayoutInflater.from(context).inflate(R.layout.follow_error, null)
                        var text = inflate.findViewById<TextView>(R.id.error_tex)
                        //添加到页面上
                        
                        adapter?.addHeaderView(inflate)
                        //协程来完成  后边加载进度

                    }


                }

            })
    }


}